package com.wujw.netty.bio;

import com.wujw.netty.util.ScriptUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Desc:
 *
 * @author wujw
 * @email jiwenwu@outlook.com
 * @date 2018/11/13
 */
public class BIOServerHandler implements Runnable{
    private Socket socket;

    public BIOServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            writer = new PrintWriter(this.socket.getOutputStream(),true);
            String body = null;
            while (true) {
                body = reader.readLine();
                if (body == null){
                    break;
                }
                System.out.println("server服务端接收参数：" + body);
                writer.println(body + "=" + ScriptUtil.cal(body));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null){
                writer.close();
            }
        }
        try {
            if (reader != null){
                reader.close();
            }
            if (socket != null){
                socket.close();
                socket = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
